package com.tencreat.mall.controller.admin;

import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.domain.account.AccountWithdrawInfo;
import com.tencreat.domain.account.MallUser;
import com.tencreat.domain.mall.MallOrder;
import com.tencreat.domain.mall.MallOrderGoods;
import com.tencreat.domain.mall.SupplierWithdrawAudit;
import com.tencreat.domain.mall.dto.MallSupplierFinanceDTO;
import com.tencreat.domain.mall.vo.ResOrderDetailInfoVO;
import com.tencreat.service.account.IAccountWithdrawInfoService;
import com.tencreat.service.account.IMallUserService;
import com.tencreat.service.account.ISupplierWithdrawAuditService;
import com.tencreat.service.mall.IMallOrderGoodsService;
import com.tencreat.service.mall.IMallOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 供应商-财务记录Controller
 *
 * @author tencreat
 * @date 2021-12-25
 */
@Api(tags="供应商-财务记录api")
@RestController
@RequestMapping("/system/finance")
public class AdminMallSupplierFinanceController extends BaseController {
    @Autowired
    private IMallUserService mallUserService;
    @Autowired
    private IAccountWithdrawInfoService withdrawInfoService;

    @Autowired
    private IMallOrderService mallOrderService;
    @Autowired
    private IMallOrderGoodsService orderGoodsService;
    @Autowired
    private ISupplierWithdrawAuditService supplierWithdrawAuditService;


    /**
     * 获取订单详情信息
     */
    @ApiOperation(value="获取订单详情信息")
    @PostMapping("/getOrderInfo")
    public HttpResult<ResOrderDetailInfoVO> getOrderInfo(@RequestBody MallSupplierFinanceDTO dto) {
        MallOrderGoods mallOrderGoods = orderGoodsService.lambdaQuery().eq(MallOrderGoods::getOrderGoodsSn, dto.getOrderSn())
                .eq(MallOrderGoods::getGoodsId, dto.getGoodsId()).eq(MallOrderGoods::getNormsId, dto.getNormsId()).one();
        MallOrder mallOrder = mallOrderService.lambdaQuery().eq(MallOrder::getOrderSn, mallOrderGoods.getOrderSn()).one();
        ReqIdParam id = new ReqIdParam();
        id.setId(mallOrder.getId());
        id.setGoodId(mallOrderGoods.getId());
        return HttpResult.ok(mallOrderService.getOrderDetail(id));
    }


    /**
     * 获取提现详情信息
     */
    @ApiOperation(value = "获取提现详情信息")
    @PostMapping(value = "/getSupplierInfo")
    public HttpResult<SupplierWithdrawAudit> getSupplierInfo(@RequestBody MallSupplierFinanceDTO dto) {
        List<SupplierWithdrawAudit> list = supplierWithdrawAuditService.lambdaQuery().eq(SupplierWithdrawAudit::getUuid, dto.getUserId())
                .eq(SupplierWithdrawAudit::getValue, dto.getAmount()).list();
        if(list.size() < 1){
            return HttpResult.ok();
        }
        SupplierWithdrawAudit supplierWithdrawAudit = list.get(0);
        String mobile = supplierWithdrawAudit.getMobile();
        MallUser user = mallUserService.lambdaQuery().eq(MallUser::getMobile, mobile).one();
        if(user == null ){
            return HttpResult.ok(supplierWithdrawAudit);
        }
        AccountWithdrawInfo accountWithdrawInfo = withdrawInfoService.lambdaQuery().eq(AccountWithdrawInfo::getUserId, user.getUuid()).oneOpt().orElse(new AccountWithdrawInfo());
        String phone = "";
        String name = "";
        String bankName = "";
        if (supplierWithdrawAudit.getWithdrawType() == 1) {
            phone = accountWithdrawInfo.getWechatPhone();
            name = accountWithdrawInfo.getWechatName();
            supplierWithdrawAudit.setQr(accountWithdrawInfo.getWechatQr());
        } else if (supplierWithdrawAudit.getWithdrawType() == 2) {
            phone = accountWithdrawInfo.getAlipayPhone();
            name = accountWithdrawInfo.getAlipayName();
            supplierWithdrawAudit.setQr(accountWithdrawInfo.getAlipayQr());
        } else {
            phone = accountWithdrawInfo.getBankCode();
            bankName = accountWithdrawInfo.getBankName();
            name = accountWithdrawInfo.getCardName();
        }
        supplierWithdrawAudit.setPhone(phone);
        supplierWithdrawAudit.setName(name);
        supplierWithdrawAudit.setBankName(bankName);
        return HttpResult.ok(supplierWithdrawAudit);
    }

}
